﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private SqlHelper sqlHelper = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        private void Bind() 
        {
            GridView1.DataSource = sqlHelper.Get("select * from StudentInfo",null);
            GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataSourceID = null;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;

            string sql = "select * from StudentInfo where stu_name like '%'+@name+'%'";

            SqlParameter[] sql1 = {new SqlParameter("@name",name)};
            GridView1.DataSource = sqlHelper.Get(sql,sql1);
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Add.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text;
            string email = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
            string intro = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
            string password = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;

            string sql = "update StudentInfo set stu_name=@name,email=@email,intro=@intro,password=@pwd where stu_id=@id";
            SqlParameter[] sqls =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro),
                new SqlParameter("@pwd",password),
                new SqlParameter("@id",id)
            };
            if (sqlHelper.Execute(sql,sqls))
            {
                GridView1.EditIndex = -1;
                Bind();
            }
            else
            {
                Response.Write("<script>alert('更新失败！')</script>");
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string sql = "delete from StudentInfo where stu_id=@id";

            SqlParameter[] sqls = { new SqlParameter("@id", id) };

            if (sqlHelper.Execute(sql,sqls))
            {
                Bind();
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
        }
    }
}